package com.wangyi.fightimg.moduls.category;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.wangyi.fightimg.R;
import com.wangyi.fightimg.moduls.main.MainFragmentBean;
import com.wangyi.fightimg.utils.http.ServiceUrl;

import java.util.List;

/**
 * @author wangYi
 *         图片列表适配器
 */
public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.MyViewHolder> {
    private Context context;
    private List<MainFragmentBean> imgBeanList;

    public ImageAdapter(Context context, List<MainFragmentBean> imgBeanList) {
        this.context = context;
        this.imgBeanList = imgBeanList;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_img, null);
        MyViewHolder myViewHolder = new MyViewHolder(view);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
//        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) holder.ivImg.getLayoutParams();
//        params.width = Integer.valueOf(imgBeanList.get(position).getWidth());
//        params.height = Integer.valueOf(imgBeanList.get(position).getHeight());
//        holder.ivImg.setLayoutParams(params);

        Glide.with(context)
                .load(ServiceUrl.BASE_IMG + imgBeanList.get(position).getUrl())
                .asBitmap()
                .placeholder(R.mipmap.ic_launcher)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .dontTransform()
                .into( holder.ivImg);
    }

    @Override
    public int getItemCount() {
        return imgBeanList.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        ImageView ivImg;
        public MyViewHolder(View itemView) {
            super(itemView);
            ivImg = (ImageView) itemView.findViewById(R.id.iv_img);
        }
    }
}
